﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Ozone.Models
{
    public class TrayCalculatorKP
    {
        private readonly PointKP[] corners;
        private const int Rows = 8;
        private const int Columns = 6;



        //离心管空瓶点位

        //TraynumberPo.Z =  408.557;
        //TraynumberPo.A = -136.743;
        //TraynumberPo.B = -1.833;
        //TraynumberPo.C = 39.144;
        public TrayCalculatorKP()
        {
            corners = new PointKP[4] {
            new PointKP(100.719f,465.535f), // 左下角
            new PointKP(104.954f,572.605f), // 右下角
            new PointKP(-71.5768f,577.217f), // 右上角
            new PointKP(-73.8884f,470.502f) // 左上角
        };
        }

        public PointKP GetPosition(int positionNumber)
        {
            if (positionNumber < 1 || positionNumber > Rows * Columns)
                throw new ArgumentOutOfRangeException("点位编号必须在1-48之间");

            // 计算行列索引(0-based)
            int col = (positionNumber - 1) / Rows;  // 列优先计算
            int row = (positionNumber - 1) % Rows;

            // Z字形排列：奇数列从下到上，偶数列从上到下
            if (col % 2 == 1)
            {
                row = Rows - 1 - row;
            }

            // 计算插值比例
            float xRatio = (float)col / (Columns - 1);
            float yRatio = (float)row / (Rows - 1);

            // 双线性插值计算坐标
            PointKP bottom = Lerp(corners[0], corners[1], xRatio);
            PointKP top = Lerp(corners[3], corners[2], xRatio);
            return Lerp(bottom, top, yRatio);
        }

        private PointKP Lerp(PointKP a, PointKP b, float t)
        {
            return new PointKP(
                a.X + (b.X - a.X) * t,
                a.Y + (b.Y - a.Y) * t);
        }
    }

    public struct PointKP
    {
        public float X { get; set; }
        public float Y { get; set; }

        public PointKP(float x, float y)
        {
            X = x;
            Y = y;
        }
    }



    ///// <summary>
    ///// 托盘点位计算
    ///// </summary>
    //public class TrayCalculatorKP
    //{
    //    // 托盘角点坐标
    //    private readonly PointKP bottomLeft;
    //    private readonly PointKP bottomRight;
    //    private readonly PointKP topLeft;
    //    private readonly PointKP topRight;

    //    // 行列数
    //    private const int Rows = 4;
    //    private const int Columns = 8;

    //    public TrayCalculatorKP()
    //    {
    //        // 初始化四个角点坐标
    //        bottomLeft = new PointKP(266.204f, 389.934f);
    //        bottomRight = new PointKP(270.697f, 571.152f);
    //        topLeft = new PointKP(194.5f, 391.46f);
    //        topRight = new PointKP(199.914f, 571.169f);
    //    }

    //    public PointKP GetPosition(int positionNumber)
    //    {
    //        if (positionNumber < 1 || positionNumber > Rows * Columns)
    //            throw new ArgumentOutOfRangeException("点位编号必须在1-32之间");

    //        // 计算行列索引(0-based)
    //        int row = (positionNumber - 1) / Columns;
    //        int col = (positionNumber - 1) % Columns;

    //        // Z字形排列：奇数行从左到右，偶数行从右到左
    //        if (row % 2 == 1)
    //        {
    //            col = Columns - 1 - col;
    //        }

    //        // 计算X和Y的插值比例
    //        float xRatio = (float)col / (Columns - 1);
    //        float yRatio = (float)row / (Rows - 1);

    //        // 计算底部和顶部的X坐标
    //        float bottomX = bottomLeft.X + (bottomRight.X - bottomLeft.X) * xRatio;
    //        float topX = topLeft.X + (topRight.X - topLeft.X) * xRatio;

    //        // 计算左侧和右侧的Y坐标
    //        float leftY = bottomLeft.Y + (topLeft.Y - bottomLeft.Y) * yRatio;
    //        float rightY = bottomRight.Y + (topRight.Y - bottomRight.Y) * yRatio;

    //        // 最终坐标是水平和垂直插值的组合
    //        float x = bottomX + (topX - bottomX) * yRatio;
    //        float y = leftY + (rightY - leftY) * xRatio;

    //        return new PointKP(x, y);
    //    }
    //}

    //public struct PointKP
    //{
    //    public float X { get; set; }
    //    public float Y { get; set; }

    //    public PointKP(float x, float y)
    //    {
    //        X = x;
    //        Y = y;
    //    }
    //}

}
